<template>
  <div class="app-container search-list">
    <table-pagination
        ref="el-search-table"
        :fetch="getData"
        type="remote"
        :page-sizes="[20, 50, 100]"
        :columns="columns"
        :form-options="formOptions"
        :adaptive = "fixOption"
        height="100px"
        list-field="records"
        total-field="totalElements"
        page-index-key="page"
        page-size-key="pageSize"
        method="get">
      <template slot="button">
        <el-divider direction="vertical"></el-divider>
        <el-button size="small" type="primary" icon="el-icon-circle-plus" @click="handleAdd">新增员工</el-button>
      </template>
      <el-table-column label="操作" slot="append" width="250px">
        <template slot-scope="scope">
<!--          <el-button v-if="scope.row.status===0" plain size="mini" type="success" @click="handleInvite(scope.row.id)">邀请</el-button>-->
          <el-button v-if="!scope.row.isAdmin" plain size="mini" type="primary" @click="handleEdit(scope.row.id)">修改</el-button>
          <el-button v-if="!scope.row.isAdmin" plain size="mini" type="danger" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </table-pagination>
  </div>
</template>

<script>
import TablePagination from '_c/Tables/table-pagination';
import { getStaffPage, deleteStaff, inviteStaff } from '@/api/site';
export default {
    name: 'staff-list',
    components: {
        TablePagination
    },
    data () {
        return {
            fixOption: {fixedHeader: true},
            list: null,
            listLoading: true,
            loadingBtn: false,
            pageParams: {
                page: 1,
                pageSize: 20,
                name: ''
            },
            formOptions: {
                showResetBtn: true,
                inline: false,
                labelWidth: 120,
                submitBtnText: '查询',
                size: 'small',
                resetBtnCallback: () => {
                    this.$refs['el-search-table'].fetchHandler(this.pageParams, false);
                },
                forms: [
                    { prop: 'name', label: '员工姓名', placeholder: '请输入员工姓名' }
                ]
            },
            columns: [
                {label: '姓名', prop: 'userName'},
                {label: '邮箱', prop: 'email'},
                {label: '手机号', prop: 'mobile'}
                // {label: '状态', prop: 'statusText'}
            ],
            tableData: []
        };
    },
    methods: {
        getData (params) {
            this.pageParams.siteId = this.siteId;
            return new Promise((resolve, reject) => {
                getStaffPage({...this.pageParams, ...params}).then((res) => {
                    resolve(res);
                });
            });
        },
        handleAdd () {
            this.$router.push({name: 'staff-add'});
        },
        handleEdit (id) {
            this.$router.push({name: 'staff-edit', params: {id}});
        },
        handleDel (id) {
            let text = '您确定要删除该员工吗？';
            this.$confirm(text, '提示',
                {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', dangerouslyUseHTMLString: true}
            ).then(() => {
                deleteStaff(id).then(res => {
                    this.$refs['el-search-table'].fetchHandler(this.pageParams, false);
                });
            }).catch(() => {
                this.$message({type: 'info', message: '已取消删除'});
            });
        },
        handleInvite (id) {
            inviteStaff({siteUserId: id}).then(res => {
                if (res.code === '200') {
                    this.$message({type: 'success', message: '邀请邮件发送成功'});
                }
            }).catch(() => {
            });
        }
    },
    computed: {
        siteId () {
            return this.$store.state.user.siteId;
        }
    }
};
</script>
